package cn.jdy.audit.admin;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.audit.entity.QqRecord;
import cn.jdy.audit.entity.SeRecord;
import cn.jdy.audit.service.QqRecordService;
import cn.jdy.common.constant.GlobalConstant;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.NginxPurgeCacheUtil;
import cn.jdy.platform.service.ConfService;

/**
 * @title 大数据搜索后台操作
 * @author jga
 * @since 1-11-2016
 */
@Controller
@RequestMapping("/qqRecord")
public class QqRecordCtrl extends BaseCtrl{
	@Autowired
	private QqRecordService qqRecordService;
	@Autowired
	private ConfService confService;
	//默认列表路径
	private static String viewName = "redirect:/qqRecord/list?s=";
	
	/**
	 *  初始化
	 * @param entity
	 * @param fromDate
	 * @param toDate
	 * @param s
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(SeRecord entity, String fromDate, String toDate, @RequestParam(required = false) Character s) {
		this.qqRecordService.updateSql();
		ModelAndView mv = new ModelAndView("/audit/qqRecord/qqRecordList.jsp");
		if(s != null)
			switch(s) {
				case '1': mv.addObject("msg", "保存成功！");break;
				case '2': mv.addObject("msg", "更新成功！");break;
				case '3': mv.addObject("msg", "删除成功！");break;
				case '4': mv.addObject("msg", "操作失败！");break;
				default: break;
			}
    mv.addObject("classList", confService.queryByType(GlobalConstant.TYPE_PRODUCTX, 0L));
		mv.addObject("entity", entity);
		return mv;
	}
	
	/**
	 * 异步查询
	 * @param sp
	 * @param fromDate
	 * @param toDate
	 * @return
	 */
	@RequestMapping("/queryByPage")
	@ResponseBody
	public Map<String, Object> queryByPage(QqRecord qqSrecord, String fromDate, String toDate){
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		if((fromDate=="" && toDate!="") || (fromDate!="" && toDate=="") || (fromDate=="" && toDate=="")){
			long current=System.currentTimeMillis();//当前时间毫秒数
			long zero=current/(1000*3600*24)*(1000*3600*24)-TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
			Date date_b=new Timestamp(zero);
			long twelve=zero+24*60*60*1000;//今天24点0分0秒的毫秒数
			Date date_e=new Timestamp(twelve);
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		    fromDate = format.format(date_b);
		    toDate=format.format(date_e);
		}
		try {
			jsonMap.put("dataList", qqRecordService.query(qqSrecord, fromDate, toDate));
			jsonMap.put("iTotalDisplayRecords", qqRecordService.count(qqSrecord, fromDate, toDate));
			jsonMap.put("iTotalRecords", qqRecordService.count());
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "列表数据查询异常", e);
		}
		return jsonMap;
	}
	
	/**
	 * 新增
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit() {
		return edit(null);
	}
	
	/**
	 * 编辑
	 * @param id
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable Integer id) {
		ModelAndView mv = new ModelAndView("/UrlRecord/UrlRecordDetail.jsp");
		System.out.println(this.confService.queryByType(GlobalConstant.TYPE_PRODUCTX, 0L).size());
		mv.addObject("column", this.confService.queryByType(GlobalConstant.TYPE_PRODUCTX, 0L));
		mv.addObject("data", id==null||id==0 ? id : this.qqRecordService.find(id));
		return mv;
	}
	
	/**
	 * 保存信息
	 * @param sp
	 * @param pid
	 * @return
	 */
	@RequestMapping("/save")
	 @ResponseBody
	public Map<String, Object> save(QqRecord qqrecord, String file) {
		ModelAndView mv = new ModelAndView();
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
		if(qqrecord.getId()==null) {//
			qqrecord.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				this.qqRecordService.save(qqrecord);
				mv.setViewName(this.viewName.concat("1"));
			} else {//更新
				this.qqRecordService.update(qqrecord);
				mv.setViewName(this.viewName.concat("2"));
			}
    jsonMap.put("statusCode", 200);
		} catch(Exception e) {
      mv.addObject("statusCode", "500");
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "信息保存异常！", e);
		}

	   return jsonMap;
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/del/{id}")
	public ModelAndView del(@PathVariable Integer id) {
		ModelAndView mv = new ModelAndView();
		try {
			this.qqRecordService.delete(id);
			 mv.setViewName(this.viewName.concat("3"));
	      // 清理内容页缓存
	      NginxPurgeCacheUtil.purge(4, id);
		} catch(Exception e) {
	     mv.setViewName(this.viewName.concat("4"));
	      BaseLog.e(getClass(), "删除异常！", e);
		}
		return mv;
	}
	/**
	 * 获取图形显示的数据 daihu
	 * @param qq_b
	 * @param qq_e
	 * @param state
	 * @param qq
	 * @param ip
	 * @return
	 */
	@RequestMapping("/analysis")
	@ResponseBody
	public Map<String, Object> analysis(String qq_b,String qq_e,String state,String qq,String ip) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		/*
		if((qq_b=="" && qq_e!="") || (qq_b!="" && qq_e=="") || (qq_b=="" && qq_e=="")){
			long current=System.currentTimeMillis();//当前时间毫秒数
			long zero=current/(1000*3600*24)*(1000*3600*24)-TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
			Date date_b=new Timestamp(zero);
			long twelve=zero+24*60*60*1000;//今天24点0分0秒的毫秒数
			Date date_e=new Timestamp(twelve);
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			qq_b = format.format(date_b);
		    qq_e=format.format(date_e);
		}*/
		DateUtil du=new DateUtil();
		int minutes=du.getAverageTime(qq_b,qq_e,6);
		try {
			//for(int i=1;i<7;i++){
				jsonMap.put("dataList1",qqRecordService.querytop1(qq_b,minutes,1,state,qq,ip));
				jsonMap.put("dataList2",qqRecordService.querytop1(qq_b,minutes,2,state,qq,ip));
				jsonMap.put("dataList3",qqRecordService.querytop1(qq_b,minutes,3,state,qq,ip));
				jsonMap.put("dataList4",qqRecordService.querytop1(qq_b,minutes,4,state,qq,ip));
				jsonMap.put("dataList5",qqRecordService.querytop1(qq_b,minutes,5,state,qq,ip));
				jsonMap.put("dataList6",qqRecordService.querytop1(qq_b,minutes,6,state,qq,ip));
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "图形数据查询异常", e);
		}
		return jsonMap;
	}
	
	@RequestMapping("/downExcel")
	public void downExcel(QqRecord qqSrecord, String fromDate, String toDate, HttpServletResponse response) {
		if((fromDate=="" && toDate!="") || (fromDate!="" && toDate=="") || (fromDate=="" && toDate=="")){
			long current=System.currentTimeMillis();//当前时间毫秒数
			long zero=current/(1000*3600*24)*(1000*3600*24)-TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
			Date date_b=new Timestamp(zero);
			long twelve=zero+24*60*60*1000;//今天24点0分0秒的毫秒数
			Date date_e=new Timestamp(twelve);
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		    fromDate = format.format(date_b);
		    toDate=format.format(date_e);
		}
		List<QqRecord> list = qqRecordService.query(qqSrecord, fromDate, toDate);
		
		for(QqRecord o : list) {
			String s;
			switch(o.getState()) {
				case 1: s = "上线"; break;
				case 2: s = "离线"; break;
				default: s = ""; break;
			}
			o.setState_name(s);
		} 
		String[] titles = new String[]{"ID", "IP", "qq号码", "mac地址", "状态", "记录时间"};
		String[][] fieldNames = new String[][]{{"id"}, {"user_ip"}, {"qq_account"},
				{"mac"},{"state_name"},{"createTime"}};
		super.downExcel(list, titles, fieldNames, "QQ查询信息", response);
	}
	
}
